package com.liu.nb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.nb.dto.DaiBanDto;
import com.liu.nb.entity.*;
import com.liu.nb.mapper.DebugtaskMapper;
import com.liu.nb.mapper.DebugtaskPoleMapper;
import com.liu.nb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 消缺任务表 服务实现类
 * </p>
 *
 * @author liuziyi
 * @since 2021-08-31
 */
@Service
public class DebugtaskServiceImpl extends ServiceImpl<DebugtaskMapper, Debugtask> implements IDebugtaskService {
    @Autowired
    DebugtaskMapper debugtaskMapper;
    @Autowired
    IPatroltaskService patroltaskService;
    @Autowired
    IReceiptpatrolService receiptpatrolService;
    @Autowired
    IUserService userService;
    @Autowired
    DebugtaskPoleMapper debugtaskPoleMapper;
    @Autowired
    IDebugtaskTypeService debugtaskTypeService;
    @Autowired
    ICircuitryService circuitryService;
    @Autowired
    IBugtypeService bugtypeService;
    @Override
    public IPage<DaiBanDto> getDaiBan(Page<DaiBanDto> page) {
        IPage<DaiBanDto> daiBan = debugtaskMapper.getDaiBan(page);
        List<DaiBanDto> daiBanDtos = daiBan.getRecords();
        daiBanDtos.forEach(i -> {
            if (i.getType().equals("消缺任务")) {
                Debugtask debugtask = this.getOne(new QueryWrapper<Debugtask>().eq("id", i.getId()));
                SysUser user = userService.getOne(new QueryWrapper<SysUser>().eq("id", debugtask.getCreateuser()).select("name"));
                debugtask.setUser(user);
                i.setDetail(debugtask);
            }
            if (i.getType().equals("巡检任务")) {
                Patroltask patroltask = patroltaskService.getOne(new QueryWrapper<Patroltask>().eq("id", i.getId()));
                patroltask.setUser(userService.getOne(new QueryWrapper<SysUser>().eq("id", patroltask.getAdminUid()).select("name")));
                patroltask.setCircuitry(circuitryService.getOne(new QueryWrapper<Circuitry>().eq("id",patroltask.getCid()).select("name")));
                i.setDetail(patroltask);
            }
            if (i.getType().equals("缺陷管理")) {
                Receiptpatrol receiptpatrol = receiptpatrolService.getOne(new QueryWrapper<Receiptpatrol>().eq("id", i.getId()));
                receiptpatrol.setBugtype(bugtypeService.getById(receiptpatrol.getBugId()));
                List<Circuitry> circuitries = circuitryService.getCircuitryByPole(receiptpatrol.getPid());
                receiptpatrol.setCidStr(circuitries.stream().map(c->c.getId()).collect(Collectors.joining(",")));
                receiptpatrol.setSysUser(userService.getOne(new QueryWrapper<SysUser>().eq("id",receiptpatrol.getFindpeople()).select("name")));
                i.setDetail(receiptpatrol);
            }
        });
        return daiBan;
    }

    /*巡检员获得待办*/
    @Override
    public IPage<DaiBanDto> getDaiBanXunJian(Page<Patroltask> page, int userId) {
        Page<Patroltask> patroltaskPage =
                patroltaskService.page(page, new QueryWrapper<Patroltask>()
                        .inSql("id", "SELECT patroltask_user.partol_id FROM patroltask_user WHERE patroltask_user.user_id = " + userId));
        /*
         * 把list转成dto
         * */
        List<DaiBanDto> daiBanListDto = new ArrayList<>();
        patroltaskPage.getRecords().forEach(i -> {
            DaiBanDto daiBanDto = new DaiBanDto();
            i.setUser(userService.getOne(new QueryWrapper<SysUser>().eq("id", i.getAdminUid()).select("name")));
            i.setCircuitry(circuitryService.getOne(new QueryWrapper<Circuitry>().eq("id",i.getCid()).select("name")));
            daiBanDto.setDetail(i);
            daiBanDto.setType("巡检任务");
            daiBanDto.setTime(i.getCreartime());
            daiBanDto.setName(i.getName());
            daiBanDto.setId(i.getId());
            daiBanListDto.add(daiBanDto);
        });
        Page<DaiBanDto> daiBanListPage = new Page<>();
        daiBanListPage.setRecords(daiBanListDto);
        daiBanListPage.setTotal(patroltaskPage.getTotal());
        return daiBanListPage;
    }

    @Override
    public IPage<DaiBanDto> getDaiBanXiaoQue(Page<Debugtask> page, int userId) {
        Page<Debugtask> debugtaskPage = this.page(page, new QueryWrapper<Debugtask>()
                .inSql("id", "SELECT did FROM debugtask_user WHERE uid =" + userId));
        List<DaiBanDto> daiBanDtoList = new ArrayList<>();
        debugtaskPage.getRecords().forEach(i -> {
            DaiBanDto daiBanDto = new DaiBanDto();
            daiBanDto.setId(i.getId());
            i.setUser(userService.getOne(new QueryWrapper<SysUser>().eq("id", i.getCreateuser()).select("name")));
            daiBanDto.setDetail(i);
            daiBanDto.setName(i.getName());
            daiBanDto.setTime(i.getTime());
            daiBanDto.setType("消缺任务");
            daiBanDtoList.add(daiBanDto);
        });
        Page<DaiBanDto> daiBanDtoPage = new Page<>();
        daiBanDtoPage.setRecords(daiBanDtoList);
        daiBanDtoPage.setTotal(debugtaskPage.getTotal());
        return daiBanDtoPage;
    }


    /**
     * @author  杨泽霖
     * @date  2021-09-09 13:56:02
     */
    @Override
    public IPage<Debugtask> getTask2(Debugtask debugtask, String cid, Integer pageNo, Integer size) {
            QueryWrapper<Debugtask>  queryWrapper =
                    new QueryWrapper<Debugtask>().eq(StrUtil.isNotBlank(debugtask.getId()), "id", debugtask.getId())
                            .eq(ObjectUtil.isNotNull(debugtask.getState()), "state", debugtask.getState())
                            .eq(ObjectUtil.isNotNull(debugtask.getCreateuser()), "createUser", debugtask.getCreateuser())
                            .between(ObjectUtil.isNotNull(debugtask.getTime()), "time", debugtask.getFirstTime(), debugtask.getLastTime())
                            .inSql(ObjectUtil.isNotNull(cid),"id", "select debug_id from debugtask_pole where debugtask_pole.ptid in(SELECT id FROM patroltask where patroltask.cid like '%"+ cid +"%')");

        Page<Debugtask> page = this.page(new Page<>(pageNo, size), queryWrapper);
        page.getRecords().forEach(i -> {
            /**
             * 给执行人赋值
             */
            List<SysUser> list = userService.list(new QueryWrapper<SysUser>()
                    .inSql("id","select uid from debugtask_user where debugtask_user.did = '" + i.getId() + "'"));
            i.setSysUser(list);
            /*
             * 给创建人赋值
             * */
            i.setUser(userService.getById(i.getCreateuser()));
            /**
             * 给单据赋值
             */
            i.setDebugtaskType(debugtaskTypeService.getById(i.getType()));
        });

        return page;
    }


    @Override
    public Integer saveDeBugTask(Debugtask debugtask) {
        return debugtaskMapper.insert(debugtask);
    }

    @Override
    public Integer updateDeBugTask(Debugtask debugtask) {
        return debugtaskMapper.updateById(debugtask);
    }



}
